<template>
    <div class="container">
        <div class="search">
            <City></City>
            <div class="content">
                <img src="@/assets/images/search-grey.png">
                <input class="f14" type="text" v-model.trim="searchContent" placeholder="想租什么，搜一搜">
                <div class="clean" @click="cleanValue" v-show="isActive">
                    <span></span>
                    <span></span>
                </div>
            </div>
            <div class="btn" @click="sendSeachRequest" :class="{ active: isActive }">
                <span class="f14">搜索</span>
            </div>
        </div>
        <div class="history" v-show="history.length">
            <div class="head">
                <span class="title f14">历史搜索</span>
                <img class="delete" src="@/assets/images/delete.png" @click="cleanHistory">
            </div>
            <div class="list">
                <div class="item f14" v-for="(item, index) in history" :key="index" @click="switchRoute(item)">
                    {{ item }}
                </div>
            </div>
        </div>
        <div class="hot-search">
            <span class="title f14" :class="{ active: history.length }">热门搜索</span>
            <div class="list b">
                <div class="item f14" v-for="item in list" :key="item" @click="switchRoute(item)">{{ item }}</div>
            </div>
        </div>
    </div>
</template>
<script>
import City from "@/components/GoodsList/City.vue"

export default {
    name: "SearchView",
    data() {
        return {
            searchContent: "",
            isActive: false,
            history: [],
            list: [],
        }
    },
    components: {
        City,
    },
    created() {
        // this.getHotSearch();
    },
    watch: {
        searchContent(value) {
            this.isActive = value.length != 0
        }
    },
    methods: {
        getHotSearch() {
            this.$sendGet("Index/keywords")
                .then(res => {
                    if (res.data.status === 200) {
                        const { data } = res.data;
                        this.list.push(...data)
                    }
                })
        },
        cleanValue() {
            this.searchContent = "";
        },
        sendSeachRequest() {
            const title = this.searchContent.trim();
            if (!title) {
                this.searchContent = "";
            } else {
                this.switchRoute(title)
            }
        },
        cleanHistory() {
            this.history.splice(0);
            localStorage.setItem("history", JSON.stringify(this.history));
        },
        switchRoute(name) {
            const index = this.history.findIndex(item => item == name);
            if (index != -1) this.history.splice(index, 1)
            this.history.unshift(name);
            localStorage.setItem("history", JSON.stringify(this.history));
            this.$router.push({ name: "list", query: { name } })
        }
    },
    mounted() {
        const array = JSON.parse(localStorage.getItem("history")) || [];
        this.history.push(...array);
    }
}
</script>
<style lang="scss" scoped>
.container {
    padding: 15px;
    padding-right: 30px;

    .search {
        display: flex;
        margin-top: 10px;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;

        .content {
            display: flex;
            position: relative;

            img {
                position: absolute;
                width: 25px;
                left: 5px;
                top: 2px;
                height: 25px;
            }

            input {
                border-radius: 15px;
                padding: 7.5px 0;
                padding-left: 30px;
                background-color: #fafafa;
                outline: 0;
                border: 0;

                &::placeholder {
                    font-size: 14px;
                }
            }

            .clean {
                width: 15px;
                height: 15px;
                // background-color: #333333;
                background-color: #979797;
                position: absolute;
                right: 10px;
                top: 50%;
                transform: translateY(-50%) rotateZ(45deg);
                border-radius: 50%;

                span {
                    position: absolute;
                    display: inline-block;
                    width: 1.5px;
                    height: 8px;
                    border-radius: 2px;
                    background-color: rgb(255, 255, 255);
                    left: 50%;
                    top: 50%;
                    transform: translate(-50%, -50%);

                    &:nth-of-type(1) {
                        transform: translate(-50%, -50%) rotateZ(88deg);
                    }
                }
            }
        }

        .btn {
            width: 48px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            color: white;
            border-radius: 12px;
            background-color: #d3d3d3;
            font-weight: bold;

            &.active {
                background-color: #f62221;
            }
        }
    }

    .hot-search {
        .title {
            display: inline-block;
            margin-top: 30px;
            margin-bottom: 15px;

            &.active {
                margin-top: 10px;
            }
        }

        .list {
            .item {
                border-radius: 20px;
                padding: 7px;
                padding-left: 25px;
                margin-bottom: 15px;
                background-color: #f9f9f9;
            }
        }
    }

    .history {
        margin-top: 30px;

        .head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 10px;

            .delete {
                width: 20px;
                object-fit: contain;
            }
        }

        .list {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;

            .item {
                padding: 6px 14px;
                margin-right: 10px;
                border-radius: 10px;
                background-color: #f9f9f9;
                margin-bottom: 10px;
            }
        }
    }
}
</style>